//-----------------------------------------------------------------------
// <copyright file="tblCurrencyInOpicDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblCurrencyInOpicDAOBase
    {
        #region Common methods
        public virtual tblCurrencyInOpic CreatetblCurrencyInOpicFromReader(IDataReader reader)
        {
            tblCurrencyInOpic item = new tblCurrencyInOpic();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (int)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"BuyCurrency")) item.BuyCurrency = (string)reader["BuyCurrency"];
                if (UtilityDAO.CheckColumnData(reader,"SellCurrency")) item.SellCurrency = (string)reader["SellCurrency"];
                if (UtilityDAO.CheckColumnData(reader,"Status")) item.Status = (int)reader["Status"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblCurrencyInOpicFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblCurrencyInOpic methods
            
        public virtual void CreatetblCurrencyInOpic(tblCurrencyInOpic tblCurrencyInOpic)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblCurrencyInOpicsInsert");
                
                database.AddInParameter(dbCommand, "@BuyCurrency", DbType.AnsiString, tblCurrencyInOpic.BuyCurrency);
                database.AddInParameter(dbCommand, "@SellCurrency", DbType.AnsiString, tblCurrencyInOpic.SellCurrency);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblCurrencyInOpic.Status);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblCurrencyInOpic.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblCurrencyInOpicException, ex);
            }
        }

        #endregion

        #region UpdatetblCurrencyInOpic methods
        
        public virtual void UpdatetblCurrencyInOpic(tblCurrencyInOpic tblCurrencyInOpic)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblCurrencyInOpicsUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, tblCurrencyInOpic.ID);
                database.AddInParameter(dbCommand, "@BuyCurrency", DbType.AnsiString, tblCurrencyInOpic.BuyCurrency);
                database.AddInParameter(dbCommand, "@SellCurrency", DbType.AnsiString, tblCurrencyInOpic.SellCurrency);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblCurrencyInOpic.Status);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblCurrencyInOpicException, ex);
            }
        }
        
        #endregion

        #region DeletetblCurrencyInOpic methods
        public virtual void DeletetblCurrencyInOpic(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblCurrencyInOpicsDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblCurrencyInOpicException, ex);
            }
        }

        #endregion

        #region GettblCurrencyInOpic methods
        
        public virtual tblCurrencyInOpic GettblCurrencyInOpic(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblCurrencyInOpicsGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                tblCurrencyInOpic tblCurrencyInOpic =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblCurrencyInOpic = CreatetblCurrencyInOpicFromReader(reader);
                        reader.Close();
                    }
                }
                return tblCurrencyInOpic;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblCurrencyInOpicException, ex);
            }
        }
        
        #endregion

        #region GettblCurrencyInOpicList methods
        public virtual tblCurrencyInOpicCollection GettblCurrencyInOpicList(tblCurrencyInOpicColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblCurrencyInOpicsGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblCurrencyInOpicCollection tblCurrencyInOpicCollection = new tblCurrencyInOpicCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblCurrencyInOpic tblCurrencyInOpic = CreatetblCurrencyInOpicFromReader(reader);
                        tblCurrencyInOpicCollection.Add(tblCurrencyInOpic);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblCurrencyInOpicCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblCurrencyInOpicListException, ex);
            }
        }
        
        public virtual tblCurrencyInOpicCollection GettblCurrencyInOpicList(tblCurrencyInOpicColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblCurrencyInOpicList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}